using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Application.Dtos.Descriptions;
using UwinEducation.Domain.Entities;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 描述管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("basic.description")]
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DescriptionController : ControllerBase
    {
        private readonly IDescriptionService _descriptionService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="descriptionService">描述服务</param>
        public DescriptionController(IDescriptionService descriptionService)
        {
            _descriptionService = descriptionService;
        }

        /// <summary>
        /// 获取描述详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>描述详情</returns>
        /// <response code="200">成功获取描述详情</response>
        /// <response code="404">描述不存在</response>
        [HttpPost]
        public async Task<ApiResponse<DescriptionDto>> GetById([FromBody] IdRequest request)
        {
            var description = await _descriptionService.GetByIdAsync(request.Id);
            return ApiResponse<DescriptionDto>.Ok(description);
        }

        /// <summary>
        /// 分页查询描述
        /// </summary>
        /// <param name="request">分页请求参数</param>
        /// <returns>分页结果</returns>
        /// <response code="200">成功获取分页数据</response>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<DescriptionDto>>> GetPagedList([FromBody] DescriptionPageRequest request)
        {
            var result = await _descriptionService.GetPagedAsync(request);
            return ApiResponse<PagedResult<DescriptionDto>>.Ok(result);
        }

        /// <summary>
        /// 创建描述
        /// </summary>
        /// <param name="createDto">创建描述DTO</param>
        /// <returns>创建后的描述详情</returns>
        /// <response code="201">成功创建描述</response>
        /// <response code="400">请求参数验证失败</response>
        [HttpPost]
        [RequirePermission("basic.description")]
        public async Task<ApiResponse<DescriptionDto>> Create([FromBody] CreateDescriptionDto createDto)
        {
            var description = await _descriptionService.CreateAsync(createDto);
            return ApiResponse<DescriptionDto>.Ok(description);
        }

        /// <summary>
        /// 更新描述
        /// </summary>
        /// <param name="updateDto">更新描述DTO</param>
        /// <returns>更新后的描述详情</returns>
        /// <response code="200">成功更新描述</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="404">描述不存在</response>
        [HttpPost]
        [RequirePermission("basic.description")]
        public async Task<ApiResponse<DescriptionDto>> Update([FromBody] UpdateDescriptionDto updateDto)
        {
            var description = await _descriptionService.UpdateAsync(updateDto);
            return ApiResponse<DescriptionDto>.Ok(description);
        }

        /// <summary>
        /// 删除描述
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>无内容</returns>
        /// <response code="204">成功删除描述</response>
        /// <response code="404">描述不存在</response>
        [HttpPost]
        [RequirePermission("basic.description")]
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _descriptionService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }
    }
}